package com.longrise.android.byjk.plugins.tabthird.invoice;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.event.InvoiceManagerEvent;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;

import org.greenrobot.eventbus.EventBus;


/**
 * Created by Administrator on 2017/10/22.
 * 开票历史详情
 */

public class BillDetailActivity extends BaseActivity2 implements SwipeRefreshLayout.OnRefreshListener {

    private TextView mExpressName;
    private TextView mExpressNum;
    private TextView mHeaderInfo;
    private TextView mInvoiceType;
    private TextView mInvoiceHeader;
    private TextView mInvoiceNum;
    private TextView mInvoiceContent;
    private TextView mTaxpayerType;
    private TextView mInvoiceMount;
    private TextView mAddressee;
    private TextView mTelNum;
    private TextView mReceiveAdd;
    private TextView tv_bill_detail_notice;
    private TextView tv_open_bank_c;
    private TextView tv_bank_account_c;
    private TextView tv_address_c;
    private TextView tv_telphone_c;
    private TextView tv_company_name_c;
    private View view_header_bill_detail;
    private RecyclerView mCostDetailRcv;
    private BillDetailRcvAdapter mRcvAdapter;

    public static final String INVOICE_STATE = "invoiceState";


    public static final String INVOICE_ID = "invoiceid";
    private LinearLayout ll_bill_notice;
    private LinearLayout ll_bill_mail;
    private LinearLayout ll_bill_num;
    private LinearLayout ll_invoice_header;
    private LinearLayout ll_buy_info_c;
    private View header_view1;

    //开票历史的id
    private String mBillId;

    //开票历史的id
    private String mBillState;

    private EntityBean mBillDetailbeans;
    //费用详情的bean
    private EntityBean[] mAccountDetailbeans;
    //快递类型
    private String expressName;

    private BBswipeRefreshLayout mSwipeRefreshLayout;

    private LinearLayout mHeaderView = null;

    //抬头类型
    private String headerType;

    //发票类型
    private String invtype;

    //发票内容
    private String contentType;

    private int flags;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_bill_detail;
    }

    @Override
    public void initView() {
        mSwipeRefreshLayout = (BBswipeRefreshLayout)findViewById(R.id.home_fragment_swipelayout);
        mCostDetailRcv = (RecyclerView) findViewById(R.id.rcv_cost_detail);

        //头部header
        mHeaderView = (LinearLayout) View.inflate(mContext, R.layout.header_bill_detail_view, null);

        mExpressName = (TextView)mHeaderView.findViewById(R.id.tv_express_name);
        mExpressNum = (TextView) mHeaderView.findViewById(R.id.tv_express_num);
        mHeaderInfo = (TextView) mHeaderView.findViewById(R.id.tv_header_info);
        mInvoiceType = (TextView) mHeaderView.findViewById(R.id.tv_invoice_type);
        mInvoiceHeader = (TextView) mHeaderView.findViewById(R.id.tv_invoice_header);
        mInvoiceNum = (TextView) mHeaderView.findViewById(R.id.tv_invoice_num);
        mInvoiceContent = (TextView) mHeaderView.findViewById(R.id.tv_invoice_content);
        mTaxpayerType = (TextView) mHeaderView.findViewById(R.id.tv_taxpayer_type);
        mInvoiceMount = (TextView) mHeaderView.findViewById(R.id.tv_invoice_mount);
        mAddressee = (TextView) mHeaderView.findViewById(R.id.tv_addressee);
        mTelNum = (TextView) mHeaderView.findViewById(R.id.tv_tel_num);
        mReceiveAdd = (TextView) mHeaderView.findViewById(R.id.tv_receive_address);
        ll_bill_notice = (LinearLayout) mHeaderView.findViewById(R.id.ll_bill_notice);
        ll_bill_mail = (LinearLayout) mHeaderView.findViewById(R.id.ll_bill_mail);
        ll_bill_num = (LinearLayout) mHeaderView.findViewById(R.id.ll_bill_num);
        ll_invoice_header = (LinearLayout) mHeaderView.findViewById(R.id.ll_invoice_header);
        ll_buy_info_c = (LinearLayout) mHeaderView.findViewById(R.id.ll_buy_info_c);
        header_view1 = mHeaderView.findViewById(R.id.header_view1);
        tv_bill_detail_notice = (TextView) mHeaderView.findViewById(R.id.tv_bill_detail_notice);
        view_header_bill_detail = mHeaderView.findViewById(R.id.view_header_bill_detail);
        tv_open_bank_c = (TextView) mHeaderView.findViewById(R.id.tv_open_bank_c);
        tv_bank_account_c = (TextView) mHeaderView.findViewById(R.id.tv_bank_account_c);
        tv_address_c = (TextView) mHeaderView.findViewById(R.id.tv_address_c);
        tv_telphone_c = (TextView) mHeaderView.findViewById(R.id.tv_telphone_c);
        tv_company_name_c = (TextView) mHeaderView.findViewById(R.id.tv_company_name_c);
        getExtraData();
        initRcv();
        clientRequest();
        setToolbarTitle(AppUtil.getString(R.string.bill_history_detail));

    }

    public void setToolbarTitle(String title) {
        mToolbar.setTitle(title);
    }

    @Override
    public void onToolbarBackClick() {
        switch (flags){
            case InvoiceConfirmActivity.INVOICE_CONFIRM_ACTIVITY:
                InvoiceManagerEvent event = new InvoiceManagerEvent();
                EventBus.getDefault().post(event);//刷新界面
                startActivity(InvoiceManagerActivity.class);
                break;

            case BillHistoryActivity.BILL_HISTORY_ACTIVITY:
                finish();
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            switch (flags){
                case InvoiceConfirmActivity.INVOICE_CONFIRM_ACTIVITY:
                    InvoiceManagerEvent event1 = new InvoiceManagerEvent();
                    EventBus.getDefault().post(event1);//刷新界面
                    startActivity(InvoiceManagerActivity.class);
                    break;

                case BillHistoryActivity.BILL_HISTORY_ACTIVITY:
                    finish();
                    break;
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void initRcv(){
        mSwipeRefreshLayout.setOnRefreshListener(this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mCostDetailRcv.setLayoutManager(linearLayoutManager);
        mRcvAdapter = new BillDetailRcvAdapter();
        mRcvAdapter.setHeaderView(mHeaderView);
        mCostDetailRcv.setAdapter(mRcvAdapter);
    }

    private void getExtraData() {
        Intent intent = getIntent();
        flags = intent.getFlags();
        mBillId = intent.getStringExtra(INVOICE_ID);
        mBillState = intent.getStringExtra(INVOICE_STATE);

        switch (flags){
            case InvoiceConfirmActivity.INVOICE_CONFIRM_ACTIVITY:
                setToolbarBackIcon(R.drawable.umeng_by_ic_close_black);
                break;

            case BillHistoryActivity.BILL_HISTORY_ACTIVITY:
                setToolbarBackIcon(R.drawable.ic_back_main);
                break;
        }

    }

    private void clientRequest() {
        EntityBean bean = new EntityBean();
        bean.set("id", mBillId);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "searchPreinvoiceByID", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    parse(result);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {
                mSwipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    private void parse(Object result) {

        EntityBean bean = (EntityBean) result;
        mBillDetailbeans = (EntityBean) bean.get("result");
        mAccountDetailbeans = (EntityBean[]) mBillDetailbeans.get("chargebeans");
        mRcvAdapter.refreshData(mAccountDetailbeans);
        setData2View(mBillDetailbeans);
    }

    public void setData2View(EntityBean bean){

        String customertype= bean.get("customertype").toString();
        String invtype= bean.get("invtype").toString();
        String invstate= bean.get("invstate").toString();
        String customername= bean.get("customername").toString();
        String invoiceitem= bean.get("invoiceitem").toString();
        String taxpayertype= bean.get("taxpayertype").toString();
        String amt= bean.get("amt").toString();
        String addressee= bean.get("addressee").toString();
        String mobile= bean.get("mobile").toString();
        String sendaddress= bean.get("sendaddress").toString();

        //增值税专用发票，显示购买方信息
         if(AppUtil.getString(R.string.invoice_type_special).equals(invtype)) {
             ll_buy_info_c.setVisibility(View.VISIBLE);
             view_header_bill_detail.setVisibility(View.VISIBLE);
             String companyName= bean.get("zzs_companyname").toString();
             String openBank= bean.get("zzs_openbyzbank").toString();
             String bankcardno= bean.get("zzs_bankcardno").toString();
             String companyAddress= bean.get("zzs_address").toString();
             String companyNum= bean.get("zzs_phone").toString();
             tv_open_bank_c.setText(openBank);
             tv_bank_account_c.setText(bankcardno);
             tv_address_c.setText(companyAddress);
             tv_telphone_c.setText(companyNum);
             tv_company_name_c.setText(companyName);
        }else if(AppUtil.getString(R.string.invoice_type_normal).equals(invtype)){
             view_header_bill_detail.setVisibility(View.GONE);
             ll_buy_info_c.setVisibility(View.GONE);
         }

        if(invstate!=null){
            //处理中
            if(invstate.equals("1")){
                ll_bill_notice.setBackgroundColor(Color.parseColor("#FFFAEDD4"));
                tv_bill_detail_notice.setText(AppUtil.getString(R.string.bill_in_progres));
                tv_bill_detail_notice.setTextColor(Color.parseColor("#FFFBA140"));
                ll_bill_mail.setVisibility(View.GONE);
                ll_bill_num.setVisibility(View.GONE);
                header_view1.setVisibility(View.GONE);
            }else if(invstate.equals("2")){
                ll_bill_notice.setBackgroundColor(Color.parseColor("#FF1BA6FF"));
                tv_bill_detail_notice.setText(AppUtil.getString(R.string.bill_mail));
                tv_bill_detail_notice.setTextColor(Color.parseColor("#FFFFFFFF"));
                ll_bill_mail.setVisibility(View.VISIBLE);
                ll_bill_num.setVisibility(View.VISIBLE);
                header_view1.setVisibility(View.VISIBLE);
                String trackingname= bean.get("trackingname").toString();
                String trackingnum= bean.get("trackingnumber").toString();
                String billno= bean.get("billno").toString();
                mExpressName.setText(trackingname);
                mExpressNum.setText(trackingnum);
                mInvoiceNum.setText(billno);
            }
        }else {
            ll_bill_mail.setVisibility(View.GONE);
            ll_bill_num.setVisibility(View.GONE);
            header_view1.setVisibility(View.GONE);
        }

        mInvoiceHeader.setText(customername);
        mInvoiceHeader.post(new Runnable() {
            @Override
            public void run() {
                int lineCount = mInvoiceHeader.getLineCount();
                ViewGroup.LayoutParams layoutParams = ll_invoice_header.getLayoutParams();
                if (lineCount >= 2) {
                    layoutParams.height = AppUtil.dip2px(71);
                } else {
                    layoutParams.height = AppUtil.dip2px(48);
                }
                ll_invoice_header.setLayoutParams(layoutParams);
            }
        });

        mHeaderInfo.setText(customertype);
        mInvoiceType.setText(invtype);
        mInvoiceContent.setText(invoiceitem);
        mTaxpayerType.setText(taxpayertype);
        mInvoiceMount.setText("¥" + amt);
        mAddressee.setText(addressee);
        mTelNum.setText(mobile);
        mReceiveAdd.setText(sendaddress);
    }

    @Override
    public void onRefresh() {
        clientRequest();
    }



}
